package com.yongqi.duocai.controller;


import com.google.gson.Gson;
import com.yongqi.duocai.entity.Invoice;
import com.yongqi.duocai.entity.param.InvoiceParam;
import com.yongqi.duocai.service.InvoiceService;
import com.yongqi.duocai.utils.JsonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 夏亮辉
 * @since 2021-06-22
 */
@RestController
@RequestMapping("/duocai/invoice")
public class InvoiceController {

    @Autowired
    private InvoiceService invoiceService;

    @GetMapping("/query")
    @ApiOperation("查询当前用户的所有发票信息")
    public JsonResult queryByUserId(Integer userId) throws InterruptedException {
        JsonResult jsonResult = new JsonResult();
        List<Invoice> invoices = invoiceService.queryByUserId(userId);
        if(invoices.size() > 0){
            jsonResult.setCode(200);
            jsonResult.setData(invoices);
        }else if(invoices.size() <= 0){
            jsonResult.setCode(200);
            jsonResult.setMessage("当前用户，没有发票信息");
        }
        return jsonResult;
    }

    @GetMapping("/query/default")
    @ApiOperation("查询当前用户的默认发票信息")
    public JsonResult queryDefault(Integer userId){
        JsonResult jsonResult = new JsonResult();
        Invoice invoice = invoiceService.queryDefault(userId);
        if(invoice != null){
            jsonResult.setCode(200);
            jsonResult.setData(invoice);
        }else {
            jsonResult.setCode(200);
            jsonResult.setMessage("当前用户，没有默认发票信息。");
        }
        return jsonResult;
    }

    @PostMapping("/add")
    @ApiOperation("新增发票信息信息")
    public JsonResult add(Integer userId,InvoiceParam param){
        JsonResult jsonResult = new JsonResult();
        Invoice invoice = new Invoice();
        BeanUtils.copyProperties(param,invoice);
        invoice.setUserId(userId);

        List<Invoice> tragets = invoiceService.queryOne(invoice);
        if(tragets.size() > 0){
            jsonResult.setCode(200);
            jsonResult.setMessage("当前信息已存在，请勿重复添加！");
            return jsonResult;
        }

        // 查询当前用户的开票信息
        List<Invoice> invoices = invoiceService.queryByUserId(userId);
        if(invoices.size() == 0){
            // 说明当前发票信息为第一条
            invoice.setIsDefault(true);
            invoice.setIsDelete(false);
            boolean res = invoiceService.save(invoice);
            if(res){
                jsonResult.setCode(200);
                jsonResult.setMessage("添加成功！");
                return jsonResult;
            }else {
                jsonResult.setMessage("添加失败！");
            }
        }else if(invoices.size() > 0){

            invoice.setIsDefault(false);
            invoice.setIsDelete(false);
            boolean res = invoiceService.save(invoice);
            if(res){
                jsonResult.setCode(200);
                jsonResult.setMessage("添加成功！");
                return jsonResult;
            }else {
                jsonResult.setMessage("添加失败！");
            }
        }
        return jsonResult;
    }

    @PutMapping("/update")
    @ApiOperation("修改发票信息")
    public JsonResult update(Integer id, InvoiceParam param){
        JsonResult jsonResult = new JsonResult();
        Invoice invoice = new Invoice();
        invoice.setId(id);
        BeanUtils.copyProperties(param,invoice);
        boolean res = invoiceService.updateById(invoice);
        if(res){
            jsonResult.setCode(200);
            jsonResult.setMessage("修改成功");
        }else {
            jsonResult.setMessage("修改失败");
        }
        return jsonResult;
    }

    @PutMapping("/update/default")
    @ApiOperation("将某个发票信息设置为默认发票抬头")
    public JsonResult updateDefault(Integer useId,Integer id){
        JsonResult jsonResult = new JsonResult();
        Invoice query = invoiceService.queryDefault(useId);
        if(query.getIsDefault()){
            Map<String,Object> code = new HashMap<>();
            code.put("isDefault", query.getIsDefault());
            Gson gson = new Gson();
            String str = gson.toJson(code);
            jsonResult.setCode(200);
            jsonResult.setData(str);
            jsonResult.setMessage("设置失败");
            return jsonResult;
        }
        query.setIsDefault(false);
        Invoice condition = invoiceService.getById(id);
        condition.setIsDefault(true);
        boolean re = invoiceService.updateById(query);
        boolean res = invoiceService.updateById(condition);
        if(res && res){
            jsonResult.setCode(200);
            jsonResult.setMessage("设置成功！");
        }else {
            jsonResult.setMessage("设置失败");
        }
        return jsonResult;

    }

    @DeleteMapping("/delete")
    @ApiOperation("删除发票信息")
    public JsonResult delete(Integer id){
        JsonResult jsonResult = new JsonResult();
        boolean res = invoiceService.removeById(id);
        if(res){
            jsonResult.setCode(200);
            jsonResult.setMessage("删除成功！");
        }else {
            jsonResult.setMessage("删除失败请稍后重试！！");
        }
        return jsonResult;
    }

}

